package io.bdmc.modules.sys.controller;

import java.time.LocalDateTime;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.bdmc.common.annotation.SysLogger;
import io.bdmc.common.utils.MyPage;
import io.bdmc.common.utils.R;
import io.bdmc.modules.sys.entity.SysDictData;
import io.bdmc.modules.sys.model.SysDictDataParam;
import io.bdmc.modules.sys.service.ISysDictDataService;

/**
 * <p>
 * 字典数据表 前端控制器
 * </p>
 *
 * @author 黄露
 * @since 2021-04-28
 */

// "字典数据表", description = "字典数据表")
@RestController
@RequestMapping("/sys/dictData")
public class SysDictDataController extends AbstractController {

	@Autowired
	ISysDictDataService _sysdictdataSvs;

	@GetMapping("/list")
	// "分页查询字典数据表", notes = "分页查询字典数据表")
	public R list(SysDictDataParam params) {
		MyPage<SysDictData> myPage = new MyPage<SysDictData>(params.getCurrent(), params.getSize());
		myPage = _sysdictdataSvs.mySelectPage(myPage, params);
		return R.ok().putPageData(myPage.getRecords(), myPage.getTotal());

	}

	@GetMapping(value = "/type/{dictType}")
	public R dictType(@PathVariable String dictType) {
		List<SysDictData> data = _sysdictdataSvs.list(new QueryWrapper<SysDictData>().eq("dict_type", dictType));
		return R.ok().putData(data);
	}

	@SysLogger("新增字典数据表")
	@PostMapping("/add")
	// "新增字典数据表", notes = "新增字典数据表")
	public R add(@RequestBody SysDictData sysdictdata) {
		sysdictdata.setCreateBy(getUser().getUsername());
		sysdictdata.setCreateTime(LocalDateTime.now());
		Boolean b = sysdictdata.insert();
		return R.trueOrFalse(b);
	}

	@SysLogger("修改字典数据表")
	@PostMapping("/update")
	// "修改字典数据表", notes = "修改字典数据表")
	public R update(@RequestBody SysDictData sysdictdata) {
		Boolean b = sysdictdata.updateById();
		return R.trueOrFalse(b);
	}

	@SysLogger("删除字典数据表")
	@DeleteMapping("/delete/{dictCode}")
	// "删除字典数据表", notes = "删除字典数据表")
	public R delete(@PathVariable Integer dictCode) {
		if (dictCode > 0)
			_sysdictdataSvs.removeById(dictCode);
		return R.ok();
	}
}